package com.cardvalue.sys.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.cardvalue.sys.activity.AddressActivity;
import com.cardvalue.sys.activity.LoadWebPage;
import com.cardvalue.sys.R;
import com.cardvalue.sys.adapter.MoreRecylerAdapter;
import com.cardvalue.sys.base.BaseFragment;
import com.cardvalue.sys.entity.MoreListDto;
import com.cardvalue.sys.newnetwork.Config;
import com.cardvalue.sys.tool.DividerGridItemDecoration;
import com.cardvalue.sys.tool.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Recyler来代替listview  更多页面
 * Created by cardvalue on 2016/4/5.
 */
public class MoreRecylerFragment extends BaseFragment {
    String server = Config.getWeixinIp()+"";
    private View view;
    @Bind(R.id.recyclerView) RecyclerView recyclerView;
    @Bind(R.id.header) RecyclerViewHeader header;
    private List<MoreListDto> mMoreListDto= new ArrayList<>();
    private MoreRecylerAdapter mMoreAdapter;
    private int [] icons={R.mipmap.share,R.mipmap.pruoduce,R.mipmap.honor,
            R.mipmap.contact,R.mipmap.about,R.mipmap.feedback,R.mipmap.service,R.mipmap.service};
    private String[] title={"融资攻略","公司简介","资质荣誉","联系方式","关于小企额","意见反馈","在线客服"};

    private String[] activity={"","","","","com.cardvalue.sys.activity.AboutActivity",
            "com.cardvalue.sys.activity.FeedBackActivity","com.cardvalue.sys.activity.CustomerServiceActivity"};
    String url1=server+"new/m/more/question";
    String url2=server+"/new/m/more/aboutUs?isApp=1";
    String url3=server+"/new/m/more/honor?isApp=1";
    String url4=server+"/new/m/more/contactUs?isApp=1";

    private String[] url={url1,url2,url3,url4,"","",""};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_more_recyler,container,false);
        ButterKnife.bind(this,view);
        // 创建一个线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        // 设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        header.attachTo(recyclerView, true);


        view.findViewById(R.id.as).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.CALL");
                intent.addCategory("android.intent.category.DEFAULT");
                //指定要拨打的电话号码
                intent.setData(Uri.parse("tel:" + "4008-803-803"));
                startActivity(intent);
            }
        });


        for(int i=0;i<title.length;i++){
            MoreListDto item=new MoreListDto();
            item.setTitle(title[i]);
            item.setIconIndex(icons[i]);
            item.setActivity(activity[i]);
            item.setUrl(url[i]);
            if (i == 0 || i == 1) {
                item.setShow(true);
            }
            mMoreListDto.add(item);
        }
        mMoreAdapter = new MoreRecylerAdapter(getActivity(),mMoreListDto);
        recyclerView.setAdapter(mMoreAdapter);
       // recyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
       // recyclerView.addItemDecoration(new DividerItemDecoration(AddressActivity.this,LinearLayoutManager.VERTICAL));
        mMoreAdapter.setItemClickListener(new MoreRecylerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
              //  MoreListDto item=(MoreListDto)mListView.getItemAtPosition(position);
                MoreListDto item  =mMoreListDto.get(position);
                if(!item.getActivity().equals("")){
                    try {
                        Intent   intent = new Intent(getActivity(),Class.forName(item.getActivity().toString()));
                        startActivity(intent);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }else if(!item.getUrl().equals("")){
                    Intent intent = new Intent(getActivity(),LoadWebPage.class);
                    intent.putExtra("title", item.getTitle().toString());
                    intent.putExtra("url", item.getUrl().toString());
                    startActivity(intent);
                }
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        setHeaderFields(0, R.string.more,0,0, 0,0);
        super.onActivityCreated(savedInstanceState);
    }

}
